package com.easy.service.impl;

import com.easy.zhu.LogMethod;
import com.easy.bean.AdoptBean;
import com.easy.bean.FosterBean;
import com.easy.bean.PetBean;
import com.easy.mapper.AdoptBeanDao;
import com.easy.mapper.FosterBeanDao;
import com.easy.mapper.PetBeanDao;
import com.easy.service.PetBeanService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PetServiceImpl implements PetBeanService {
    @Autowired
    PetBeanDao petBeanDao;

    @Autowired
    AdoptBeanDao adoptBeanDao;

    @Autowired
    FosterBeanDao fosterBeanDao;

    @Override
    @LogMethod(serviceName = "用户添加处理")
    public PetBean add(PetBean petBean) {
        petBeanDao.add(petBean);
        PetBean result = petBeanDao.one(petBean.getId());
        return result;
    }

    @Override
    @LogMethod(serviceName = "用户删除处理")
    public int del(Integer id) {
        return petBeanDao.del(id);
    }

    @Override
    @LogMethod(serviceName = "用户修改处理")
    public PetBean edit(PetBean petBean) {
        // 1. 先更新宠物表
        petBeanDao.edit(petBean);
        PetBean result = petBeanDao.one(petBean.getId());

        // 2. 同步更新领养表关联字段
        syncAdoptRelated(petBean.getPet_id(), petBean.getPet_name(), petBean.getState());
        // 3. 同步更新寄养表关联字段
        syncFosterRelated(petBean.getPet_id(), petBean.getPet_name(), petBean.getState());
        // 4. （若之前有照片同步，保留原逻辑）
        syncAdoptPhoto(petBean.getPet_id(), petBean.getPet_photo());
        syncFosterPhoto(petBean.getPet_id(), petBean.getPet_photo());

        return result;
    }

    @Override
    @LogMethod(serviceName = "用户查询处理")
    public PetBean one(Integer id) {
        return petBeanDao.one(id);
    }

    @Override
    @LogMethod(serviceName = "用户查询处理")
    public List<PetBean> all(PetBean petBean) {
        return petBeanDao.all(petBean);
    }

    @Override
    @LogMethod(serviceName = "用户分页处理")
    public void page(Page page, PetBean petBean) {
        int count = petBeanDao.pageCount(petBean);
        Object data = petBeanDao.page(page, petBean);
        page.setCount(count);
        page.setData(data);
    }

    // 同步领养表：宠物名、状态、领养时间
    private void syncAdoptRelated(String petId, String petName, String state) {
        AdoptBean query = new AdoptBean();
        query.setPet_id(petId);
        List<AdoptBean> adoptList = adoptBeanDao.all(query);

        for (AdoptBean adopt : adoptList) {
            adopt.setPet_name(petName);
            adopt.setAdopt_state(state);   // 同步状态到领养状态
            adoptBeanDao.edit(adopt);
        }
    }

    // 同步寄养表：宠物名、状态、寄养时间
    private void syncFosterRelated(String petId, String petName, String state) {
        FosterBean query = new FosterBean();
        query.setPet_id(petId);
        List<FosterBean> fosterList = fosterBeanDao.all(query);

        for (FosterBean foster : fosterList) {
            foster.setPet_name(petName);
            foster.setFoster_state(state); // 同步状态到寄养状态
            fosterBeanDao.edit(foster);
        }
    }

    // 原照片同步逻辑（保留）
    private void syncAdoptPhoto(String petId, String newPhoto) {
        AdoptBean query = new AdoptBean();
        query.setPet_id(petId);
        List<AdoptBean> adoptList = adoptBeanDao.all(query);

        for (AdoptBean adopt : adoptList) {
            adopt.setPet_photo(newPhoto);
            adoptBeanDao.edit(adopt);
        }
    }

    private void syncFosterPhoto(String petId, String newPhoto) {
        FosterBean query = new FosterBean();
        query.setPet_id(petId);
        List<FosterBean> fosterList = fosterBeanDao.all(query);

        for (FosterBean foster : fosterList) {
            foster.setPet_photo(newPhoto);
            fosterBeanDao.edit(foster);
        }
    }

    @Override
    public Map<String, Integer> countPetKind() {
        // 调用Mapper层方法，返回统计结果
        return petBeanDao.countPetKind();
    }

}